/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.wadl.fedora34;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import javax.activation.DataSource;
import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.apache.log4j.Logger;
import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;
import org.jvnet.ws.wadl.util.DSDispatcher;
//import org.jvnet.ws.wadl.util.JAXBDispatcher;
import org.jvnet.ws.wadl.util.UriBuilder;

import uk.ljmu.fedoraifz.restrequests.UserCredentials;

public class PidMethods {

    //private JAXBDispatcher _jaxbDispatcher;
    private DSDispatcher _dsDispatcher;
    private UriBuilder _uriBuilder;
    @SuppressWarnings("unused")
    private JAXBContext _jc;
    private HashMap<String, Object> _templateAndMatrixParameterValues;
    private List<String> matrixParamSet;
    private DataSource retVal;
    private static Logger log = Logger.getLogger(PidMethods.class);

    public void setMatrixParamSet(List<String> matrixParamSet) {
	this.matrixParamSet = matrixParamSet;
    }

    public List<String> getMatrixParamSet() {
	return matrixParamSet;
    }

    public void setRetVal(DataSource retVal) {
	this.retVal = retVal;
    }

    public DataSource getRetVal() {
	return retVal;
    }

    /**
     * Create new instance
     * 
     */
    public PidMethods(String pid)
    throws JAXBException
    {
	_dsDispatcher = new DSDispatcher();
	_uriBuilder = new UriBuilder();
	String basePath;
	try {
	    basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	} catch (JFigException e) {
	    log.error("Error: ", e);
	    //Error, use default Fedora path
	    basePath = "http://localhost:8080/fedora";
	}
	matrixParamSet = _uriBuilder.addPathSegment(basePath);
	matrixParamSet = _uriBuilder.addPathSegment("/objects");
	matrixParamSet = _uriBuilder.addPathSegment("/{pid}/methods");
	_templateAndMatrixParameterValues = new HashMap<String, Object>();
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    /**
     * Get pid
     * 
     */
    public String getPid() {
	return ((String) _templateAndMatrixParameterValues.get("pid"));
    }

    /**
     * Set pid
     * 
     */
    public void setPid(String pid) {
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    public void get()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /* public void getAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get(String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (asofdatetime != null && !asofdatetime.equals(""))
	     _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	     _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(UserCredentials userCreds, String asofdatetime, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Add credentials
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	if (asofdatetime != null && !asofdatetime.equals(""))
	     _queryParameterValues.put("asOfDateTime", asofdatetime);
	if (format != null && !format.equals(""))
	     _queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /* public void getAsvoid(String asofdatetime, String format)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("asOfDateTime", asofdatetime);
	_queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public static class SDef {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public SDef(String pid, String sdef)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();

	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/methods");
	    matrixParamSet = _uriBuilder.addPathSegment("/{sDef}");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("sDef", sdef);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get sDef
	 * 
	 */
	public String getSdef() {
	    return ((String) _templateAndMatrixParameterValues.get("sDef"));
	}

	/**
	 * Set sDef
	 * 
	 */
	public void setSdef(String sdef) {
	    _templateAndMatrixParameterValues.put("sDef", sdef);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String asofdatetime, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String asofdatetime, String format)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    if (format != null && !format.equals(""))
		_queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String asofdatetime, String format)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	    _queryParameterValues.put("format", format);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

    public static class SDefMethod {

	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public SDefMethod(String pid, String sdef, String method)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }
	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/methods");
	    matrixParamSet = _uriBuilder.addPathSegment("/{sDef}/{method}");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	    _templateAndMatrixParameterValues.put("sDef", sdef);
	    _templateAndMatrixParameterValues.put("method", method);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get sDef
	 * 
	 */
	public String getSdef() {
	    return ((String) _templateAndMatrixParameterValues.get("sDef"));
	}

	/**
	 * Set sDef
	 * 
	 */
	public void setSdef(String sdef) {
	    _templateAndMatrixParameterValues.put("sDef", sdef);
	}

	/**
	 * Get method
	 * 
	 */
	public String getMethod() {
	    return ((String) _templateAndMatrixParameterValues.get("method"));
	}

	/**
	 * Set method
	 * 
	 */
	public void setMethod(String method) {
	    _templateAndMatrixParameterValues.put("method", method);
	}

	public void get()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void get(String asofdatetime)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	public void get(UserCredentials userCreds, String asofdatetime)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Add credentials
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    if (asofdatetime != null && !asofdatetime.equals(""))
		_queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	    return ;
	}

	/*public void getAsvoid(String asofdatetime)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("asOfDateTime", asofdatetime);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

    }

}